package com.sgcc.ln.pmos.mobileapp.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.sgcc.ln.pmos.common.base.MvpBaseRecycleFragment;
import com.sgcc.ln.pmos.common.utils.DialogUtil;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.mobileapp.adapter.AccountInfoAdapter;
import com.sgcc.ln.pmos.mobileapp.bean.AccountInfoBean;
import com.sgcc.ln.pmos.mobileapp.bean.CodeBean;
import com.sgcc.ln.pmos.mobileapp.contract.AccounNoInfoContract;
import com.sgcc.ln.pmos.mobileapp.dialog.AccountCountryPopup;
import com.sgcc.ln.pmos.mobileapp.dialog.AccountDetailPopup;
import com.sgcc.ln.pmos.mobileapp.dialog.AccountIncrementPopup;
import com.sgcc.ln.pmos.mobileapp.dialog.AccountTownPopup;
import com.sgcc.ln.pmos.mobileapp.presenter.AcctNoInfoPresenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author ChaoAJ
 * @describe 关联企业信息--户号信息
 * @time 2022/10/18 11:56
 */
public class AccountNoInfoFragment extends MvpBaseRecycleFragment<AccountInfoBean.DataDTO, AcctNoInfoPresenter> implements AccounNoInfoContract.View {


    private TextView tv_add;
    AccountCountryPopup countryPopup;
    AccountTownPopup townPopup;
    AccountIncrementPopup incrementPopup;
    AccountDetailPopup detailPopup;
    Map<String, Object> params = new HashMap<>();
    AccountInfoAdapter adapter;
    private boolean isHasData = false;


    public static AccountNoInfoFragment newInstance() {
        AccountNoInfoFragment fragment = new AccountNoInfoFragment();
        return fragment;
    }

    @Override
    protected AcctNoInfoPresenter createPresenter() {
        return new AcctNoInfoPresenter();
    }

    @Override
    public boolean isShowContent() {
        return true;
    }

    @Override
    public AccountInfoAdapter getAdapter() {
        adapter = new AccountInfoAdapter();
        View footer_view = getLayoutInflater().inflate(R.layout.footer_account_add, recycleview, false);
        findViewByIds(footer_view);
        adapter.addFooterView(footer_view);
        adapter.setBtnClickListener(new AccountInfoAdapter.BtnClickListener() {
            @Override
            public void delete(int position, AccountInfoBean.DataDTO bean) {
                DialogUtil.showConfirm(getContext(), "提示", "是否删除户号？", new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        mPresenter.deletMeterPointInfo(bean.getUsePointId());
                    }
                });
            }

            @Override
            public void itemClick(int position, AccountInfoBean.DataDTO bean) {
                new XPopup.Builder(getContext())
                        .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                        .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                        .asCustom(new AccountDetailPopup(getContext(), new AccountDetailPopup.OnClickListener() {
                            @Override
                            public void onDeletSuccess() {
                                refresh(true);
                            }
                        }, bean))
                        .show();
            }
        });
        return adapter;
    }


    @Override
    public View getHeaderLayout(RelativeLayout parent) {
        View head_view = getLayoutInflater().inflate(R.layout.header_account_layout, parent, false);
        return head_view;
    }

    private void findViewByIds(View footer_view) {
        tv_add = (TextView) footer_view.findViewById(R.id.tv_add);
        tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String powerGridType = "0";//0所有可操作   1国家电网，2地方电网，3增量
                List<AccountInfoBean.DataDTO> data = adapter.getData();
                if (null != data && data.size() > 0) {
                    String p = data.get(0).getPowerGridType();
                    if (!TextUtils.isEmpty(powerGridType)) {
                        powerGridType = p;
                    }
                }
                if (powerGridType.equals("0")) {
                    showCountryPop(true);
                }
                if (powerGridType.equals("1")) {
                    showCountryPop(false);
                }
                if (powerGridType.equals("2")) {
                    showTounPop(false);
                }
                if (powerGridType.equals("3")) {
                    showIncrementPop(false);
                }


            }
        });
    }

    private void showCountryPop(boolean isClick) {
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .asCustom(new AccountCountryPopup(getContext(), isClick, new AccountCountryPopup.OnClickListener() {
                    @Override
                    public void onPositiveClick(AccountCountryPopup popup, Map<String, Object> params) {
                        mPresenter.saveMater(params);
                        countryPopup = popup;
                    }

                    @Override
                    public void onTypeClick(AccountCountryPopup popup, int position) {
                        switch (position) {
                            case 1:
                                popup.dismiss();
                                showTounPop(isClick);
                                break;

                            case 2:
                                popup.dismiss();
                                showIncrementPop(isClick);
                                break;
                        }
                    }
                }))
                .show();
    }

    private void showTounPop(boolean isClick) {
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .asCustom(new AccountTownPopup(getContext(), isClick, new AccountTownPopup.OnClickListener() {
                    @Override
                    public void onPositiveClick(AccountTownPopup popup, Map<String, Object> params) {
                        mPresenter.doMater(params);
                        townPopup = popup;
                    }

                    @Override
                    public void onTypeClick(AccountTownPopup popup, int position) {
                        switch (position) {
                            case 0:
                                popup.dismiss();
                                showCountryPop(isClick);
                                break;
                            case 2:
                                popup.dismiss();
                                showIncrementPop(isClick);
                                break;
                        }
                    }
                }))
                .show();
    }

    private void showIncrementPop(boolean isClick) {
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .asCustom(new AccountIncrementPopup(getContext(), isClick, new AccountIncrementPopup.OnClickListener() {
                    @Override
                    public void onPositiveClick(AccountIncrementPopup popup, Map<String, Object> params) {
                        mPresenter.doMater(params);
                        incrementPopup = popup;
                    }

                    @Override
                    public void onTypeClick(AccountIncrementPopup popup, int position) {
                        switch (position) {
                            case 0:
                                popup.dismiss();
                                showCountryPop(isClick);
                                break;
                            case 1:
                                popup.dismiss();
                                showIncrementPop(isClick);
                                break;
                        }
                    }
                }))
                .show();
    }


    @Override
    public void saveMaterSuccess(AccountInfoBean response) {
        if (response != null) {
            ToastUtils.showShort(response.getMessage());
            if (response.getStatus() == 0) {
                if (countryPopup != null) {
                    if (countryPopup.isShow()) {
                        countryPopup.dismiss();
                    }
                }
                if (townPopup != null) {
                    if (townPopup.isShow()) {
                        townPopup.dismiss();
                    }
                }
                if (incrementPopup != null) {
                    if (incrementPopup.isShow()) {
                        incrementPopup.dismiss();
                    }
                }
            }
            refresh(true);
        } else {
            ToastUtils.showShort("提交失败");
        }
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        //TODO 先注释调 后边打开
        mPresenter.getMaterList();
    }

    @Override
    public void getMaterListSuccess(AccountInfoBean response) {
        if (response != null) {
            if (response.getStatus() == 0) {
                isHasData = null != response.getData() && response.getData().size() > 0;
                loadSuccess(false, response.getData());
            } else {
                ToastUtils.showShort(response.getMessage());
            }
        } else {
            ToastUtils.showShort("获取列表失败");
        }

    }

    @Override
    public void deletMeterPointInfoSuccess(CodeBean response) {
        if (response != null) {
            if (response.getStatus() == 0) {
                ToastUtils.showShort(response.getMessage());
                refresh(false);
            } else {
                ToastUtils.showShort(response.getMessage());
            }
        } else {
            ToastUtils.showShort("删除失败失败");
        }

    }


    public boolean next() {
        if (!isHasData) {
            ToastUtils.showShort("请先关联户号");
            return false;
        }
        return true;
    }
}